#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=95

DEVCTL=/dev/natflow_zone_ctl

zone_idx=0

natflow_zone_setup()
{
	local idx=$zone_idx
	zone_idx=$((zone_idx+1))
	local cfg="$1"
	local fw_zone ifname type

	config_get fw_zone "$cfg" fw_zone
	config_get ifname "$cfg" ifname
	config_get type "$cfg" type

	(for fwz in $fw_zone; do
		fw3 -q zone $fwz
	done; \
	for ifn in $ifname; do
		echo $ifn
	done) | sed 's/\*$/+/' | sort | uniq | while read IFN; do
		echo $type $idx=$IFN >$DEVCTL
	done
}

start() {
	test -c $DEVCTL || return 0

	echo clean >$DEVCTL

	config_load natflow
	config_foreach natflow_zone_setup zone

	echo update_match >$DEVCTL
}

stop() {
	test -c $DEVCTL || return 0
}

restart()
{
	start
}
